package com.GroupSix.controller;

import com.GroupSix.entity.TeacherEntity;
import com.GroupSix.service.TeacherService;
import com.GroupSix.vo.TeacherVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/teacher")
public class TeacherController {

    //注入教师接口
    @Autowired
    private TeacherService teacherService;

    //返回所有的教师信息
    @GetMapping("/allTeacher")
    public List<TeacherEntity> getAllTeacher() {
        return teacherService.list();
    }

    //根据账号查询教师信息
    @GetMapping("/findBySno/{sno}")
    public TeacherEntity findBySno(@PathVariable("sno") Integer sno) {
        return teacherService.getById(sno);
    }

    //增加一个教师信息
    @PostMapping("/add")
    public String add(@RequestBody TeacherEntity teacherEntity){
        Integer i = teacherService.add(teacherEntity);
        if(i>0){
            return "success";
        }
        else {
            return "fail";
        }
    }

    //注册一个教师信息
    @PostMapping("/signUp")
    public String signUp(@RequestParam("name") String name, @RequestParam("id") Integer id,
                         @RequestParam("pwd") String pwd, @RequestParam("course") String course){
        return teacherService.signUp(name, id, pwd, course);
    }

    //根据账号删除教师信息
    @DeleteMapping("/deleteBySno/{sno}")
    public String deleteBySno(@PathVariable("sno") Integer sno){
        Integer i = teacherService.delete(sno);
        if(i>0){
            return "删除成功";
        }
        else {
            return "删除失败,该账号不存在";
        }
    }

    //根据账号修改教师信息
    @PutMapping("/update/")
    public String update(@RequestBody TeacherEntity teacher){
        Integer i = teacherService.update(teacher);
        if(i>0){
            return "success";
        }
        else {
            return "fail";
        }
    }

    //分页查询
    @GetMapping("/list")
    public IPage<TeacherEntity> list(@RequestBody TeacherVo teacherVo){
        TeacherEntity student= new TeacherEntity();
        student.setId(teacherVo.getId());
        return teacherService.list(student, teacherVo.getCurpage(), teacherVo.getLimit());
    }
}
